package work;

public class Producer extends Thread {
    Baozi baozi;
    BaoziBox baoziBox;
    boolean f = false;
    public Producer(Baozi baozi, BaoziBox baoziBox) {
        this.baozi = baozi;
        this.baoziBox = baoziBox;
    }

    @Override
    public void run() {
        while (true) {
            f = false;
            synchronized (baozi) {
                if (baoziBox.flag) {
                    try {
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                if (baoziBox.getCount() != 5) {
                    Baozi b = new Baozi();
                    if (baoziBox.count % 2 == 0) {
                        b.pi = "厚皮";
                        b.xian = "牛肉";

                    } else {
                        b.pi = "薄皮";
                        b.xian = "羊肉";

                    }
                    System.out.println(Thread.currentThread().getName() + "正在生产" + b.toString());
                    //System.out.println(b.toString());
                    baoziBox.putBaozi(b);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("包子生产完毕，剩余包子：" + baoziBox.getCount() + "个");
                    f = true;
                }


                if (baoziBox.getCount() == 5) {
                    if(f){
                        System.out.println("生产完毕，等待消费者消费");
                    }
                    baoziBox.flag = true;
                    baozi.notifyAll();
                }
            }
        }
    }


}
